package com.mall.promo.services;

import com.mall.commons.tool.utils.UtilDate;
import com.mall.promo.PromoService;
import com.mall.promo.constants.PromoRetCode;
import com.mall.promo.converter.ItemConverter;
import com.mall.promo.dal.entitys.PromoItem;
import com.mall.promo.dal.entitys.PromoSession;
import com.mall.promo.dal.persistence.PromoItemMapper;
import com.mall.promo.dal.persistence.PromoSessionMapper;
import com.mall.promo.dto.*;
import com.mall.promo.mq.PromoMessageProvider;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author
 * @date 2020/12/26 18:11
 */
@Service(interfaceClass = PromoService.class)
@Component
public class PromoServiceImpl implements PromoService {
    @Autowired
    PromoSessionMapper promoSessionMapper;
    @Autowired
    PromoItemMapper promoItemMapper;
    @Reference(check = false)
    IProductService productService;
    @Autowired
    ItemConverter itemConverter;
    @Autowired
    PromoMessageProvider promoMessageProvider;


    @Override
    public SecKillListResponse sesckilllist(SecKillListRequest request) {
        SecKillListResponse response = new SecKillListResponse();
        Integer sessionId = request.getSessionId();
        String yyyymmdd = UtilDate.getDate();
        Example example = new Example(PromoSession.class);
        example.createCriteria().andEqualTo("sessionId",sessionId).andEqualTo("yyyymmdd",yyyymmdd);
        Integer psId = null;
        List<PromoSecKillProductDetail> productList = null;
        try {
            List<PromoSession> promoSessions = promoSessionMapper.selectByExample(example);
            PromoSession promoSession = promoSessions.get(0);
            psId = promoSession.getId();
            productList = promoItemMapper.selectSecKillProductList(psId);
            for (PromoSecKillProductDetail promoSecKillProductDetail : productList) {
                String picUrl = promoSecKillProductDetail.getPicUrl();
                String[] split = picUrl.split(",");
                String url = split[0];
                promoSecKillProductDetail.setPicUrl(url);
            }

        } catch (Exception e) {
            response.setCode(PromoRetCode.SYSTEM_TIMEOUT.getCode());
            response.setMsg(PromoRetCode.SYSTEM_TIMEOUT.getMessage());
            return response;
        }
        response.setProductList(productList);
        response.setPsId(psId);
        response.setSessionId(sessionId);
        response.setCode(PromoRetCode.SUCCESS.getCode());
        response.setMsg(PromoRetCode.SUCCESS.getMessage());

        return response;
    }

    @Override
    public PromoProductDetailResponse promoProductDetail(PromoProductDetailRequest request) {
        PromoProductDetailResponse response = new PromoProductDetailResponse();
        try {
            ProductDetailRequest productDetailRequest = new ProductDetailRequest();
            productDetailRequest.setId(request.getProductId());
            ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);
            ProductDetailDto productDetailDto = productDetail.getProductDetailDto();
            PromoProductDetailDTO promoProductDetailDTO = itemConverter.itemDTO2PromoDTO(productDetailDto);
            Example example = new Example(PromoItem.class);
            example.createCriteria().andEqualTo("itemId",request.getProductId()).andEqualTo("psId",request.getPsId());
            List<PromoItem> promoItems = promoItemMapper.selectByExample(example);
            BigDecimal seckillPrice = promoItems.get(0).getSeckillPrice();
            promoProductDetailDTO.setPromoPrice(seckillPrice);
            response.setPromoProductDetailDTO(promoProductDetailDTO);
        } catch (Exception e) {
            response.setCode(PromoRetCode.SYSTEM_TIMEOUT.getCode());
            response.setMsg(PromoRetCode.SYSTEM_TIMEOUT.getMessage());
            return response;
        }
        response.setCode(PromoRetCode.SUCCESS.getCode());
        response.setMsg(PromoRetCode.SUCCESS.getMessage());
        return response;
    }

    /**
     * 秒杀下单接口
     * 扣减库存  生成订单 生成物流
     * 本地事务：扣减库存
     * 远程事务：订阅消息 根据消息来生成订单生成物流消息
     * @param request
     * @return
     */
    @Override
    public SecKillResponse seckill(SecKillRequest request) {

        Example example = new Example(PromoItem.class);
        example.createCriteria().andEqualTo("psId",request.getPsId())
                .andEqualTo("itemId",request.getProductId());
        List<PromoItem> promoItems = promoItemMapper.selectByExample(example);
        PromoItem promoItem = promoItems.get(0);
        request.setPromoPrice(promoItem.getSeckillPrice());

        SecKillResponse response = new SecKillResponse();

        boolean ret = promoMessageProvider.sendMessage(request);
        if (ret) {
            response.setCode(PromoRetCode.SUCCESS.getCode());
            response.setMsg(PromoRetCode.SUCCESS.getMessage());
        }else {
            response.setCode(PromoRetCode.SYSTEM_TIMEOUT.getCode());
            response.setMsg(PromoRetCode.SYSTEM_TIMEOUT.getMessage());
        }
            return response;
    }
}
